/*  Equalizer, version 1.0.1
 *  (c) 2007 Sven Hecht
 *
 *  Equalizer is freely distributable under the terms of an MIT-style license.
 *  For details, see http://sven.hecht.googlepages.com/index.html
 *
/*--------------------------------------------------------------------------*/

var Equalizer = Class.create();
Equalizer.prototype = {
  initialize: function(_contentdiv, _width, _height, _bars, _color, _LowDefault, _upside, _speedchangecallback, _lowChangeCallback)
  {
		this.equalizerArray = new Array('5', '7','8','10','12','13','15','16','18','19','22','23','24','28','29','30','32','33','36','37','40','44','45');
		this.equalizerlowArray = new Array('5', '6','7','8','9','10','11','12','13','14','15','16','17','18','19','20');

		this.barwidth = _width;
		this.barheight = _height;
		this.bars = _bars;
		this.speed = 100;
		this.bararray;
		this.low = _LowDefault;
		this.barcolor = _color;
		this.speedchangecallback = _speedchangecallback;
		this.lowChangeCallback = _lowChangeCallback;
		this.upside = _upside;
		this.heightfactor = _height / 20;
		this.running = true;
		
		
		var eq = $(_contentdiv);
		if (eq) {
			this.bararray = new Array();
			var eqPos = Position.cumulativeOffset(eq);
			eq.style.backgroundColor = '';
			eq.style.zIndex = '1';
			var bar;
			for (var i=0; i<this.bars; i++)
			{
				bar = document.createElement('div');
				bar.style.backgroundColor = this.barcolor;
				bar.style.height = this.barheight+'px';
				bar.style.width = (this.barwidth/this.bars-1)+'px';
				bar.style.position = 'absolute';
				bar.style.left = (i*(this.barwidth/this.bars)+eqPos[0])+'px';
				if (this.upside) {
					bar.style.bottom = 0+'px';
				}
				bar.style.display = 'block';
				bar.style.zIndex = '1';
				bar.innerHTML = '&nbsp;';
				eq.appendChild(bar);
				this.bararray[i] = bar;
			}
		}
		setTimeout(this.equalize.bind(this), this.speed);
  },

	equalize: function ()
	{
		if (this.bararray.length > 0)
		{
			var equalizer;
			var dataarray = this.low?this.equalizerlowArray:this.equalizerArray;
			for (var i=0; i<this.bararray.length; i++)
			{
				equalizer = eval(dataarray[Math.round((dataarray.length-1)*Math.random())]);
				this.bararray[i].style.height = (equalizer * this.heightfactor)+'px';
			}
			if (this.running) {
				setTimeout(this.equalize.bind(this), this.speed);
			}
		}
		if (this.speedchangecallback) {
			this.speedchangecallback();
		}
		if (this.lowChangeCallback) {
			this.lowChangeCallback();
		}
	},

	IncreaseSpeed: function ()
	{
		if (this.speed > 0) {
			this.speed -= 50;
		} else {
			this.speed = 0;
		}
		if (this.speedchangecallback) {
			this.speedchangecallback();
		}
	},
	
	DecreaseSpeed: function ()
	{
		this.speed += 50;
		if (this.speedchangecallback) {
			this.speedchangecallback();
		}
	},
	
	ToggleLow: function ()
	{
		this.low = !this.low;
		if (this.lowChangeCallback) {
			this.lowChangeCallback();
		}
	},
	
	Off: function ()
	{
		this.running = false;
	},
	
	On: function ()
	{
		if (!this.running) {
			setTimeout(this.equalize.bind(this), this.speed);
			this.running = true;
		}
	}
}